<?php defined('SYSPATH') or die('No direct script access.');

abstract class Controller_Initial_Page extends Controller_Template {
        
        public $title = 'myde.sk';
        public $description = 'ladny opis strony';
        public $keywords = array();
    
        
        public function before()
        {
            parent::before();
            
            //ACL            
            $acl = new ACL;
            
            //add roles 
            $acl->add_role('guest');
            $acl->add_role('login');
            
            //add resources
            $acl->add_resource('error_handler');
            $acl->add_resource('account');
            $acl->add_resource('contact');
            $acl->add_resource('desktop');
            $acl->add_resource('project');
            $acl->add_resource('index');
            $acl->add_resource('message');
            $acl->add_resource('search');
            $acl->add_resource('settings');
            
            
            //add rules - default everything for everybody is deny  
            $acl->allow('guest', 'error_handler');
            $acl->allow('guest', 'index');
            $acl->allow('guest', 'account');
            $acl->allow('guest', 'search');
            $acl->allow('guest', 'desktop', 'index');
            $acl->allow('guest', 'project', 'show');
            $acl->allow('guest', 'project', 'get_embed_object');
            $acl->allow('guest', 'settings', 'get_locality');
            $acl->deny('guest', 'account', 'send_invitation');
            
            //Allow "login" to do anything
            $acl->allow('login');
            
            $is_allowed = false;         
                 
            if(Auth::instance()->logged_in())
            {
                $user_model = Auth::instance()->get_user();
                foreach($user_model->roles->find_all() as $role)
                {     
                    if ($acl->is_allowed($role->name, $this->request->controller(), $this->request->action() ))
                    {
                        $is_allowed = true;
                    }
                }
                
            }
            else
            {
                if ($acl->is_allowed('guest', $this->request->controller(), $this->request->action() ))    
                {
                    $is_allowed = true;
                }
            }
            
            if(!$is_allowed)        
                throw new HTTP_Exception_404;           
            
            
            // Check if this ajax or internal request or normal
            if ($this->request->is_ajax() OR !$this->request->is_initial()) 
            {
                $this->auto_render = FALSE;
		
                if ($this->request->is_ajax())
		{
                    $this->request->action('ajax_'.$this->request->action());
                    $this->request->headers['Content-Type'] = 'application/json';
		} 
		else
		{
                    $this->request->action('internal_'.$this->request->action());
                    $this->bind_meta();
		}
            }
            else if (preg_match("/^internal|ajax/", $this->request->action())) 
            {
                throw new Internal_Exception('called internal method');
            }
            else
            {
                $this->bind_meta();
                      
            }
            
             if(Auth::instance()->logged_in())
             {
                    //set global var to View
                    //profile url for toolbar
                     View::set_global('user_url',$user_model->user_profile->url); 

                     //amounts notices fot toolbar
                     $notices_model = ORM::factory('notice');
                     $amounts_notices = $notices_model->get_notices($user_model->id);       
                     View::set_global('amounts_notices',$amounts_notices); 
              }
              
        }
        
        private function bind_meta()
        {
                              // Bind meta data 
                $this->template->bind('title',$this->title);
                $this->template->bind('description',$this->description);
                $this->template->bind('keywords', $this->keywords);
            
                // Included custom styles and scripts
                $this->template->set('styles',array());
                $this->template->set('scripts',array());
        }
        
        public function after()
        {
            return parent::after();
        }

} // End Initial Page